package com.mysmartphone.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.ImageView;

import com.mysmartphone.vistas.R;
import com.mysmartphone.vo.Phone;

public class Tools {
	public static boolean timeMessage(long time_start, long time_end,
			long time_wait) {
		boolean res = false;
		if (time_start - time_wait > time_end) {
			res = true;
		}
		return res;
	}

	public static String getLanguage(Resources res) {
		Locale locale = res.getConfiguration().locale;
		return locale.getLanguage();
	}

	public static void setLanguage(Context context, String language) {
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		android.content.res.Configuration conf = context.getResources()
				.getConfiguration();
		conf.locale = new Locale(language);
		context.getResources().updateConfiguration(conf, dm);
	}

	public static void resetApp(Context context) {
		Intent i = context.getPackageManager().getLaunchIntentForPackage(
				context.getPackageName());
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		context.startActivity(i);
	}

	public static ArrayList<String> getCategories(Context context) {

		ArrayList<String> categories = new ArrayList<String>();

		categories.add(context.getString(R.string.brand));
		categories.add(context.getString(R.string.gama));
		categories.add(context.getString(R.string.model));
		categories.add(context.getString(R.string.OS));
		categories.add(context.getString(R.string.yearAbre));
		categories.add(context.getString(R.string.nameCoreAbre));
		categories.add(context.getString(R.string.numberCoreAbre));
		categories.add(context.getString(R.string.speedCoreAbre));
		categories.add(context.getString(R.string.memoryROMabre));
		categories.add(context.getString(R.string.memoryRAMabre));
		categories.add(context.getString(R.string.batery));
		categories.add(context.getString(R.string.sdCardAbre));
		categories.add(context.getString(R.string.backCameraAbre));
		categories.add(context.getString(R.string.frontCameraAbre));
		categories.add(context.getString(R.string.flash));
		categories.add(context.getString(R.string.frecuency));
		categories.add(context.getString(R.string.conectivity));
		categories.add(context.getString(R.string.numberSim));
		categories.add(context.getString(R.string.screenSize));
		categories.add(context.getString(R.string.screenTecnology));
		categories.add(context.getString(R.string.resolution));
		categories.add(context.getString(R.string.gps));
		categories.add(context.getString(R.string.wifi));
		categories.add(context.getString(R.string.bluetooth));
		categories.add(context.getString(R.string.dimensiones));
		categories.add(context.getString(R.string.price));
		categories.add(context.getString(R.string.bechmark));

		return categories;
	}

	public static Phone jsonToPhone(JSONObject json) {

		return new Phone();
	}

	public static ArrayList<Phone> jsonToListPhone(JSONArray jsonList) {
		ArrayList <Phone>listPhone = null;
		if (jsonList != null) {
			listPhone = new ArrayList<Phone>();
			for (int i = 0; i < jsonList.length(); i++) {
				Phone phone = new Phone();
				try {

					JSONObject json = jsonList.getJSONObject(i);
					// phone.setImage(Integer.parseInt((String)
					// json.get("image")));
					//phone.setImage(R.drawable.img);
					phone.setIdPhone(Long.parseLong((String) json
							.get("idphone")));
					phone.setIdBrand(Long.parseLong((String) json.get("idBrand")));
					phone.setBrand((String) json.get("brand"));
					phone.setGama((String) json.get("gama"));
					phone.setModel((String) json.get("model"));
					phone.setOperatingSystem((String) json
							.get("operatingSystem"));
					phone.setYear(Integer.parseInt((String) json.get("year")));
					phone.setNameCore((String) json.get("nameCore"));
					phone.setNumberCore(Integer.parseInt((String) json
							.get("numberCore")));
					phone.setNumberDesCore((String) json.get("numberDesCore"));
					phone.setSpeedCore(Double.parseDouble((String) json
							.get("speedCore")));
					phone.setSpeedDesCore((String) json.get("speedDesCore"));
					phone.setMemoryROM(Double.parseDouble((String) json
							.get("memoryROM")));
					phone.setMemoryDesRom((String) json.get("memoryDesRam"));
					phone.setMemoryRAM(Double.parseDouble((String) json
							.get("memoryRAM")));
					phone.setMemoryDesRam((String) json.get("memoryDesRam"));
					phone.setBattery(Double.parseDouble((String) json
							.get("battery")));
					phone.setBatteryDes((String) json.get("batteryDes"));
					phone.setSdCard(Boolean.parseBoolean((String) json
							.get("sdCard")));
					phone.setBackCamera(Double.parseDouble((String) json
							.get("backCamera")));
					phone.setFrontCamera(Double.parseDouble((String) json
							.get("frontCamera")));
					phone.setCameraDes((String) json.get("cameraDes"));
					phone.setFlash(Boolean.parseBoolean((String) json
							.get("flash")));
					phone.setFrecuency((String) json.get("frecuency"));
					phone.setConectivity((String) json.get("conectivity"));
					phone.setNumberSim(Integer.parseInt((String) json
							.get("numberSim")));
					phone.setScreenSize(Double.parseDouble((String) json
							.get("screenSize")));
					phone.setScreenTecnology((String) json
							.get("screenTecnology"));
					phone.setResolution((String) json.get("resolution"));
					phone.setGps(Boolean.parseBoolean((String) json.get("gps")));
					phone.setWifi(Boolean.parseBoolean((String) json
							.get("wifi")));
					phone.setBluetooth(Boolean.parseBoolean((String) json
							.get("bluetooth")));
					phone.setDimensions((String) json.get("dimensions"));
					phone.setPrice(Double.parseDouble((String) json
							.get("price")));
					phone.setBechmark(Double.parseDouble((String) json
							.get("bechmark")));

					listPhone.add(phone);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		return listPhone;
	}

	public static JSONObject makeHttpRequest(String url, String method,
			List<NameValuePair> params) {
		InputStream is = null;
		JSONObject jObj = null;
		String json = "";
		// Making HTTP request
		try {

			// check for request method
			if (method == "POST") {
				// request method is POST
				// defaultHttpClient
				DefaultHttpClient httpClient = new DefaultHttpClient();
				HttpPost httpPost = new HttpPost(url);
				httpPost.setEntity(new UrlEncodedFormEntity(params));

				HttpResponse httpResponse = httpClient.execute(httpPost);
				HttpEntity httpEntity = httpResponse.getEntity();
				is = httpEntity.getContent();

			} else if (method == "GET") {
				// request method is GET
				DefaultHttpClient httpClient = new DefaultHttpClient();
				String paramString = URLEncodedUtils.format(params, "utf-8");
				url += "?" + paramString;
				HttpGet httpGet = new HttpGet(url);

				HttpResponse httpResponse = httpClient.execute(httpGet);
				HttpEntity httpEntity = httpResponse.getEntity();
				is = httpEntity.getContent();
			}

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			json = sb.toString();
		} catch (Exception e) {
			Log.e("Buffer Error", "Error converting result " + e.toString());
		}

		// try parse the string to a JSON object
		try {
			jObj = new JSONObject(json);
		} catch (JSONException e) {
			Log.e("JSON Parser", "Error parsing data " + e.toString());
		}

		// return JSON String
		return jObj;
	}


}
